﻿using Cimbalino.Phone.Toolkit.Services;
using GalaSoft.MvvmLight.Command;
using LightOMator.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace LightOMator.ViewModel
{
    public class EditViewModel : BusyViewModel
    {

        private ObservableFadeCollection _fadeCollection;
        public ObservableFadeCollection Fades
        {
            get { return _fadeCollection; }
            set
            {
                _fadeCollection = value;
                RaisePropertyChanged();
            }
        }

        private bool _isFadeModeDirect;
        public bool IsFadeModeDirect
        {
            get { return _isFadeModeDirect; }
            set
            {
                if (value != _isFadeModeDirect)
                {
                    _isFadeModeDirect = value;
                    RaisePropertyChanged();
                }
            }
        }

        private bool _isFadeModeBlack;
        public bool IsFadeModeBlack
        {
            get { return _isFadeModeBlack; }
            set
            {
                if (value != _isFadeModeBlack)
                {
                    _isFadeModeBlack = value;
                    RaisePropertyChanged();
                }
            }
        }

        private bool _isFadeModeNone;
        public bool IsFadeModeNone
        {
            get { return _isFadeModeNone; }
            set
            {
                if (value != _isFadeModeNone)
                {
                    _isFadeModeNone = value;
                    RaisePropertyChanged();
                }
            }
        }


        public RelayCommand AddColorCommand { get; private set; }
        public RelayCommand RemoveColorCommand { get; private set; }
        public RelayCommand SaveFadesCommand { get; private set; }
        public RelayCommand DeleteFadesCommand { get; private set; }

        public EditViewModel()
        {
            AddColorCommand = new RelayCommand(addColor);
            RemoveColorCommand = new RelayCommand(removeColor);
            SaveFadesCommand = new RelayCommand(saveCollection);
            DeleteFadesCommand = new RelayCommand(deleteCollection);

            if (IsInDesignMode)
            {
                Fades = new ObservableFadeCollection
                {
                    Name = "My Fades",
                    FadeMode = FadeMode.DirectFade,
                    Fades = new ObservableCollection<ObservableFade>(new ObservableFade[]{
                        new ObservableFade{ Color = Colors.Red, FadeInTimeMs = 2000, KeepOnTimeMs = 4000 },
                        new ObservableFade{ Color = Colors.Green, FadeInTimeMs = 0, KeepOnTimeMs = 8000 },
                        new ObservableFade{ Color = Colors.Blue, FadeInTimeMs = 3000, KeepOnTimeMs = 30 * 1000 }
                    })
                };
                IsFadeModeDirect = true;
            }
        }

        private void deleteCollection()
        {
            throw new NotImplementedException();
        }

        private void saveCollection()
        {
            Fades.ToFadeCollection(FadeCollection.CurrentFadeCollection);
            goBack();
        }

        private void goBack()
        {
            IsolatedStorageSettings.ApplicationSettings.Save();
            NavigationService service = new NavigationService();
            service.GoBack();
        }

        public override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Fades = new ObservableFadeCollection(FadeCollection.CurrentFadeCollection);
            IsFadeModeDirect = FadeCollection.CurrentFadeCollection.FadeMode == FadeMode.DirectFade;
            IsFadeModeBlack = FadeCollection.CurrentFadeCollection.FadeMode == FadeMode.DarkFade;
            IsFadeModeNone = FadeCollection.CurrentFadeCollection.FadeMode == FadeMode.NoFade;
        }

        private void removeColor()
        {
            throw new NotImplementedException();
        }

        private void addColor()
        {
            throw new NotImplementedException();
        }
    }
}
